using System.Data;
	using System.Collections.Generic;
	using System.Reflection;
	using System.Linq;
	using System.Linq.Expressions;
	using System.ComponentModel;
	using System;
	
using GameSchool.Models;


namespace GameSchool.DAL
{
    public class VideoRepository : IVideoRepository, IDisposable
    {
        // Nánari upplýsingar um hvernig Repository patternið virkar er á:
        // http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

        /// <summary>
        /// Meðlimabreyta sem er gagnagrunnstengingin
        /// </summary>
        private gsEntities m_db;


        /// <summary>
        /// Class Initializer sem tekur við gagnagrunnstengingu sem skal nota
        /// </summary>
        /// <param name="db"></param>
        public VideoRepository(gsEntities db)
        {
            this.m_db = db;
        }

        /// <summary>
        /// Skilar lista yfir öll prófin
        /// </summary>
        /// <returns></returns>
        public List<Video> GetVideo()
        {
            return m_db.Video.ToList();
        }

        /// <summary>
        /// Sækir eitt próf eftir ID
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public Video GetVideoById(int videoId)
        {
            return m_db.Video.SingleOrDefault(e => e.VideoID == videoId);
        }

        /// <summary>
        /// Bætir við prófi í gagnagrunninn. ATH: Vistar ekki sjálfkrafa
        /// </summary>
        /// <param name="Video"></param>
        public void InsertVideo(Video video)
        {
            m_db.Video.AddObject(video);
        }

        /// <summary>
        /// Eyðir prófi úr gagnagrunninum. ATH: Vistar ekki sjálfkrafa
        /// </summary>
        /// <param name="videoId"></param>
        public void DeleteVideo(int videoId)
        {
            // Við skulum ekkert gera sérstakt hérna þar sem við viljum ekki leyfa að eyða prófum.
            // Annars væri kóðinn fyrir það svona:

            //var toDelete = m_db.Video.SingleOrDefault(e => e.VideoID == videoId);
            //m_db.Video.DeleteObject(toDelete);
            //m_db.SaveChanges();
        }

        /// <summary>
        /// Uppfærir próf í gagnagrunninum. ATH: Vistar ekki sjálfkrafa
        /// </summary>
        /// <param name="video"></param>
        public void UpdateVideo(Video video)
        {
            m_db.Video.Attach(video);
        }

        //addacommenti
        /*
        public void InsertComment(VideoComments videocomments)
        {
            c.CommentDate = DateTime.Now;
            double milli = c.CommentDate.Millisecond;
            // We don't need (or want) millisecond precision), so we remove it.
            c.CommentDate = c.CommentDate.AddMilliseconds(-milli);
            m_db.VideoComments.CreateObject<VideoComments>();
        }
        */
        public Course GetCourseById(int courseId)
        {
            return m_db.Course.SingleOrDefault(i => i.CourseID == courseId);
        }


        
        public void InsertVideoComment(VideoComments videocomments)
        {
            m_db.VideoComments.AddObject(videocomments);
        }

        public List<VideoComments> GetComments(int videoId)
        {
            return m_db.VideoComments.Where(i => i.VideoID == videoId).ToList();
        }

        public void DeleteComment(int videoCommentId)
        {
            var toDelete = m_db.VideoComments.SingleOrDefault(e => e.VideoCommentID == videoCommentId);
            m_db.VideoComments.DeleteObject(toDelete);
            m_db.SaveChanges();
        }

        /// <summary>
        /// Vistar breytingar sem gerðar voru í sessioninu
        /// </summary>
        public void Save()
        {

            m_db.SaveChanges();
            

        }

        #region Disposerinn fyrir using() statement - Alltaf eins
        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    m_db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
    